//
//  ABCollectionView.m
//  无线轮播
//
//  Created by 本 on 16/4/21.
//  Copyright © 2016年 本. All rights reserved.
//

#import "ABCollectionView.h"
#import "UIImageView+WebCache.h"
static NSString * const ID = @"hi";
static NSInteger const count = 10;
/*****************ABCollectionViewCell开始***********************/

@interface ABCollectionViewCell : UICollectionViewCell
@property (nonatomic, weak)UIImageView *imageView;
@end
@implementation ABCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [self.contentView addSubview:imageView];
        _imageView = imageView;
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.imageView.frame = self.bounds;
}
@end


/*****************ABCollectionViewCell结束***********************/



/*****************ABCollectionView***********************/

@interface ABCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, weak)UICollectionView *collectionView;
@property (nonatomic, strong)NSTimer *timer;
@end
@implementation ABCollectionView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        
        UICollectionView *view = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        view.delegate = self;
        view.dataSource = self;
        view.showsHorizontalScrollIndicator = NO;
        view.showsVerticalScrollIndicator = NO;
        view.pagingEnabled = YES;
        [view registerClass:[ABCollectionViewCell class] forCellWithReuseIdentifier:ID];
        
        
        _collectionView = view;
        [self addSubview:view];

    }
    return self;
}
- (void)setImageArray:(NSArray *)imageArray{
    _imageArray = imageArray;
    
    if (self.imageArray.count > 1) {
        
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
    NSInteger integer = count * imageArray.count / 2  ;
//         [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:(XMGItemCount * images.count) / 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:integer inSection:0] atScrollPosition:0 animated:NO];
    });

    [self startTimer];
    
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    layout.itemSize = self.bounds.size;

}
- (void)startTimer{
    _timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
    
}
- (void)stopTimer{
    [self.timer invalidate];
    self.timer = nil;
    
}
- (void)nextPage{

    CGPoint contentOffest =  self.collectionView.contentOffset;
    contentOffest.x += self.collectionView.bounds.size.width;
    [self.collectionView setContentOffset:contentOffest animated:YES];
    
}
- (void)setupCollection{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(375, 150);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    UICollectionView *view = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, 375, 150) collectionViewLayout:layout];
    view.delegate = self;
    view.dataSource = self;
    view.backgroundColor = [UIColor blueColor];
    view.showsHorizontalScrollIndicator = NO;
    view.showsVerticalScrollIndicator = NO;
    view.pagingEnabled = YES;
    _collectionView = view;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.imageArray.count == 1)
        return 1;
    else
        return self.imageArray.count * count;

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ABCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    id unknown = self.imageArray[indexPath.item % self.imageArray.count];
    if ([unknown isKindOfClass:[NSString class]]) {
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[indexPath.item % self.imageArray.count]]];
        
    }else{

    UIImage *image = self.imageArray[indexPath.item % self.imageArray.count];
        cell.imageView.image = image;}
    return cell;
    
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self startTimer];
    
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self stopTimer];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    [self dealScroll];
}
- (void)dealScroll{
    if (self.imageArray.count > 1) {
        
    
    NSInteger page =  _collectionView.contentOffset.x / self.collectionView.bounds.size.width;
    NSInteger integer = count * self.imageArray.count / 2 + (page % self.imageArray.count) ;
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:integer inSection:0] atScrollPosition:0 animated:NO];
        
    }
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self dealScroll];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.delegate respondsToSelector:@selector(didSelectCollectionView:cellForItemAtIndexPath:)]) {
        [self.delegate didSelectCollectionView:self cellForItemAtIndexPath:indexPath.item % self.imageArray.count];
    }

}
/*****************ABCollectionView***********************/
@end
